package area

import (
	"Project/global"
	"Project/model/area/request"
	"Project/model/common/response"
	"Project/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"log"
	"strconv"
)

type ShopApi struct {}

// ShopList 药店信息列表
func (s *ShopApi) ShopList(c *gin.Context) {
	// 获取用户id
	userID := utils.GetUserID(c)
	data,err := shopService.ShopList(userID)
	if err != nil {
		log.Println("获取药店列表失败",err)
		global.GVA_LOG.Error("获取药店列表失败",zap.Error(err))
		response.FailWithMessage("获取药店列表失败",c)
		return
	}
	response.OkWithDetailed(data,"获取成功",c)
}
// ShopUserList 查看药店人员列表
func (s *ShopApi) ShopUserList(c *gin.Context) {
	shopID,err := strconv.ParseInt(c.Query("shop_id"),10,64)
	if err != nil {
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	data,err := shopService.ShopUserList(shopID)
	if err != nil {
		log.Println("获取药店人员列表失败",err)
		global.GVA_LOG.Error("获取药店人员列表失败",zap.Error(err))
		response.FailWithMessage("获取药店人员列表失败",c)
		return
	}
	response.OkWithDetailed(data,"查询成功",c)
}

// DelSHop 删除药店信息
func (s *ShopApi) DelSHop(c *gin.Context) {
	shopID,err := strconv.ParseInt(c.Query("shop_id"),10,64)
	if err != nil {
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	if err = shopService.DelSHop(shopID); err != nil {
		log.Println("删除失败",err)
		global.GVA_LOG.Error("删除失败",zap.Error(err))
		response.FailWithMessage("删除失败",c)
		return
	}
	response.OkWithMessage("删除成功",c)
}

// EditorShop 修改药店信息
func (s *ShopApi) EditorShop(c *gin.Context) {
	var shopRes request.ShopRes
	if err := c.ShouldBindJSON(&shopRes);err != nil{
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	err := shopService.EditorShop(shopRes)
	if err != nil {
		log.Println("修改药店人员列表失败",err)
		global.GVA_LOG.Error("修改药店人员列表失败",zap.Error(err))
		response.FailWithMessage("修改药店人员列表失败",c)
		return
	}
	response.Ok(c)
}

// CreateDrugShop 创建店铺
func (s *ShopApi) CreateDrugShop(c *gin.Context)  {
	var drugShop request.DrugShop
	if err := c.ShouldBindJSON(&drugShop); err != nil {
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	userID := utils.GetUserID(c)
	err := shopService.CreateDrugShop(userID,drugShop)
	if err != nil {
		log.Println("创建药店失败",err)
		global.GVA_LOG.Error("创建药店失败",zap.Error(err))
		response.FailWithMessage("创建药店失败",c)
		return
	}
	response.Ok(c)
}

// CreateShopKeeper 创建店主
func (s *ShopApi) CreateShopKeeper(c *gin.Context)  {
	var keeper request.DrugKeeper
	if err := c.ShouldBindJSON(&keeper); err != nil {
		log.Println("获取参数失败",err)
		global.GVA_LOG.Error("获取参数失败",zap.Error(err))
		response.FailWithMessage("参数绑定失败",c)
		return
	}
	err := shopService.CreateShopKeeper(keeper)
	if err != nil {
		log.Println("创建药店失败",err)
		global.GVA_LOG.Error("创建药店失败",zap.Error(err))
		response.FailWithMessage("创建药店失败",c)
		return
	}
	response.Ok(c)
}


